# Maintainer: Sergey Popov <popov.sa@gmail.com>
# Contributor: Mario Emmenlauer <memmenlauer@biodataanalysis.de>

_realname=vtk
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=9.4.2
pkgrel=2
pkgdesc="A software system for 3D computer graphics, image processing and visualization (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.vtk.org/"
msys2_repository_url="https://gitlab.kitware.com/vtk/vtk"
msys2_references=(
  'archlinux: vtk'
  "cpe: cpe:/a:vtk:vtk"
)
license=("spdx:BSD-3-Clause")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-tbb"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libharu"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openxr-sdk"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         $([[ ${MSYSTEM} == MINGW64 ]] || echo "${MINGW_PACKAGE_PREFIX}-verdict")
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-adios2"
             "${MINGW_PACKAGE_PREFIX}-alembic"
             $([[ ${MSYSTEM} == MINGW64 ]] || echo "${MINGW_PACKAGE_PREFIX}-anari-sdk")
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-cgns"
             "${MINGW_PACKAGE_PREFIX}-cli11"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-fast_float"
             "${MINGW_PACKAGE_PREFIX}-ffmpeg"
             "${MINGW_PACKAGE_PREFIX}-fontconfig"
             "${MINGW_PACKAGE_PREFIX}-gdal"
             "${MINGW_PACKAGE_PREFIX}-gl2ps"
             "${MINGW_PACKAGE_PREFIX}-hdf5"
             "${MINGW_PACKAGE_PREFIX}-liblas"
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             "${MINGW_PACKAGE_PREFIX}-libpng"
             "${MINGW_PACKAGE_PREFIX}-libogg"
             "${MINGW_PACKAGE_PREFIX}-libtheora"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-netcdf"
             "${MINGW_PACKAGE_PREFIX}-openslide"
             "${MINGW_PACKAGE_PREFIX}-opencascade"
             "${MINGW_PACKAGE_PREFIX}-openturns"
             "${MINGW_PACKAGE_PREFIX}-openvdb"
             "${MINGW_PACKAGE_PREFIX}-openvr"
             "${MINGW_PACKAGE_PREFIX}-pdal"
             # "${MINGW_PACKAGE_PREFIX}-pegtl" https://gitlab.kitware.com/vtk/vtk/-/issues/18151
             "${MINGW_PACKAGE_PREFIX}-postgresql"
             "${MINGW_PACKAGE_PREFIX}-proj"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
             "${MINGW_PACKAGE_PREFIX}-sqlite3"
             # "${MINGW_PACKAGE_PREFIX}-tk"
             "${MINGW_PACKAGE_PREFIX}-unixodbc"
             "${MINGW_PACKAGE_PREFIX}-utf8cpp"
             )
optdepends=("${MINGW_PACKAGE_PREFIX}-adios2: I/O ADIOS2"
            "${MINGW_PACKAGE_PREFIX}-alembic: I/O Alembic"
            "${MINGW_PACKAGE_PREFIX}-boost-libs: InfovisBoost and InfovisBoostGraphAlgorithms modules"
            "${MINGW_PACKAGE_PREFIX}-cgns: CGNS Reader"
            "${MINGW_PACKAGE_PREFIX}-ffmpeg: I/O FFMPEG"
            "${MINGW_PACKAGE_PREFIX}-fontconfig: Fontconfig Rendering"
            "${MINGW_PACKAGE_PREFIX}-gdal: I/O GDAL"
            "${MINGW_PACKAGE_PREFIX}-gl2ps: PS Rendering"
            "${MINGW_PACKAGE_PREFIX}-hdf5: I/O HDF5"
            "${MINGW_PACKAGE_PREFIX}-liblas: I/O LAS"
            "${MINGW_PACKAGE_PREFIX}-libmariadbclient: I/O MySQL/MariaDB"
            "${MINGW_PACKAGE_PREFIX}-libpng: I/O PNG Images"
            "${MINGW_PACKAGE_PREFIX}-libogg: I/O OGG and Theora"
            "${MINGW_PACKAGE_PREFIX}-libtheora: I/O OGG and Theora"
            "${MINGW_PACKAGE_PREFIX}-libxml2: I/O Infovis"
            "${MINGW_PACKAGE_PREFIX}-netcdf: I/O netCDF"
            "${MINGW_PACKAGE_PREFIX}-opencascade: I/O OCCT"
            "${MINGW_PACKAGE_PREFIX}-openslide: DomainsMicroscopy"
            "${MINGW_PACKAGE_PREFIX}-openturns: OpenTURNS Filter"
            "${MINGW_PACKAGE_PREFIX}-openvdb: I/O OpenVDB"
            "${MINGW_PACKAGE_PREFIX}-openvr: OpenVR Rendering"
            "${MINGW_PACKAGE_PREFIX}-pdal: I/O PDAL"
            "${MINGW_PACKAGE_PREFIX}-postgresql: I/O PostgreSQL"
            "${MINGW_PACKAGE_PREFIX}-proj: GEOVis"
            "${MINGW_PACKAGE_PREFIX}-python: Python bindings"
            "${MINGW_PACKAGE_PREFIX}-python-matplotlib: Matplotlib renderer"
            "${MINGW_PACKAGE_PREFIX}-qt6-base: Qt Rendering"
            "${MINGW_PACKAGE_PREFIX}-qt6-declarative: Qt Qml plugin"
            "${MINGW_PACKAGE_PREFIX}-sqlite3: I/O SQL"
            # "${MINGW_PACKAGE_PREFIX}-tk: TCL bindings, Python Tk widgets"
            "${MINGW_PACKAGE_PREFIX}-unixodbc")
if [[ ${MSYSTEM} != MINGW64 ]]; then
  optdepends+=("${MINGW_PACKAGE_PREFIX}-anari-sdk: ANARI Rendering")
fi
source=(https://www.vtk.org/files/release/${pkgver%.*}/VTK-${pkgver}.tar.gz
        "001-no-exact-version-for-fast_float.patch"
        "002-llvm-19-compatibility.patch"
        "003-fix-build-with-fmt-11.patch"
        "004-accept-future-versions-of-boost-and-occ.patch"
        "005-remove-unused-method-from-mir-tables.patch"
        "006-fix-compile-error-for-contour-tree-print.patch"
        "007-dll-export-some-functions.patch"
        "008-update-to-diy-3.6.0.patch"
        "009-diy-3-compatibility.patch"
        "011-anari-integer-types.patch"
        "012-fix-build-with-netcdf-4.9.3.patch")
sha256sums=('36c98e0da96bb12a30fe53708097aa9492e7b66d5c3b366e1c8dc251e2856a02'
            '411b0521fcd2864acae0af20ba6334f69f7c605c9e31996529f64aa749d61065'
            '410ddfbb2acb66746771a0e1f3ad81230904f712729af29cd26e8aaaa19dc48e'
            '2d1795111b410e0fbb8e7b08fab2e83e37e74ca0d7fd6685d435d4b27f355b87'
            'a44b3ba4bdc02be4767ff48b8a7fd4206a53756a881421fca99f538b08a7b48c'
            '66e3ac19197fb9f6f49dc98a746919ed025619ab8dae433d153c22ca595d92bc'
            'ace675cf8a9081532756b66d4f1ca1b03b12dad719b79e8cfa60ee6d60e2eeaf'
            '6cc0c44b2cb9693b8ae101fdde186561633fc6e6629c6e797231331895baa5df'
            '7602c7b4afa61f57f27022d4ea015c6e3c1d4ab8df5907ca828e08bb48c26b20'
            '19ffd04b06b4c3c0c3f0b1b375cbac956191d4b7d3fa1066dc62bbfe78d69908'
            'd76aa1788bbe2dd14390d4c7a4e65bcd652e4521324a7982ef7ead2306c82436'
            'eabe075c71ea6bbabfe3d0f1bf10f01ec299c88a8a825866f944eeb0b067aa2e')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  apply_patch_with_msg \
    001-no-exact-version-for-fast_float.patch \
    002-llvm-19-compatibility.patch \
    003-fix-build-with-fmt-11.patch \
    004-accept-future-versions-of-boost-and-occ.patch \
    005-remove-unused-method-from-mir-tables.patch \
    006-fix-compile-error-for-contour-tree-print.patch \
    008-update-to-diy-3.6.0.patch \
    009-diy-3-compatibility.patch \
    011-anari-integer-types.patch \
    012-fix-build-with-netcdf-4.9.3.patch

  # Succeeds with Clang but fails with GCC!
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    apply_patch_with_msg \
      007-dll-export-some-functions.patch
  fi
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MSYSTEM} == MINGW64 ]]; then
    _extra_config+=("-DVTK_MODULE_ENABLE_VTK_RenderingAnari=NO"
                    "-DVTK_MODULE_USE_EXTERNAL_VTK_verdict=OFF")
  fi

  CFLAGS+=" ${CPPFLAGS}"
  CXXFLAGS+=" ${CPPFLAGS} -D_USE_MATH_DEFINES -w"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_INSTALL_LICENSEDIR=share/licenses/vtk \
    -DBUILD_SHARED_LIBS=ON \
    -DPython3_EXECUTABLE="${MINGW_PREFIX}/bin/python.exe" \
    -DVTK_BUILD_TESTING=OFF \
    -DVTK_VERSIONED_INSTALL=OFF \
    -DVTK_BUILD_ALL_MODULES=ON \
    -DVTK_GROUP_ENABLE_MPI=NO \
    -DVTK_GROUP_ENABLE_Web=NO \
    -DVTK_MODULE_ENABLE_VTK_FiltersParallelVerdict=WANT \
    -DVTK_MODULE_ENABLE_VTK_FiltersTemporal=WANT \
    -DVTK_MODULE_ENABLE_VTK_IOERF=WANT \
    -DVTK_MODULE_ENABLE_VTK_IOFDS=WANT \
    -DVTK_MODULE_ENABLE_VTK_RenderingOpenXRRemoting=NO \
    -DVTK_MODULE_ENABLE_VTK_RenderingZSpace=NO \
    -DVTK_USE_EXTERNAL=ON \
    -DVTK_MODULE_USE_EXTERNAL_VTK_exprtk=OFF \
    -DVTK_MODULE_USE_EXTERNAL_VTK_ioss=OFF \
    -DVTK_MODULE_USE_EXTERNAL_VTK_pegtl=OFF \
    -DVTK_MODULE_USE_EXTERNAL_VTK_token=OFF \
    -DVTK_WRAP_JAVA=OFF \
    -DVTK_WRAP_PYTHON=ON \
    -DVTK_SMP_IMPLEMENTATION_TYPE=TBB \
    -DVTK_SMP_ENABLE_SEQUENTIAL=ON \
    -DVTK_SMP_ENABLE_STDTHREAD=ON \
    -DVTK_SMP_ENABLE_OPENMP=ON \
    -DVTK_USE_VIDEO_FOR_WINDOWS=ON \
    -DVTK_ENABLE_OSPRAY=OFF \
    -DVTK_ENABLE_REMOTE_MODULES=OFF \
    -DVTK_IGNORE_CMAKE_CXX11_CHECKS=ON \
    "${_extra_config[@]}" \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/Copyright.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  # Remove third party CMake patching for older versions than ours
  rm -rv "${pkgdir}"${MINGW_PREFIX}/lib/cmake/vtk/patches/3.{1{3,6,9},2{0,2,3}}
  # … as well as duplicate copies
  rm -rv "${pkgdir}"${MINGW_PREFIX}/lib/cmake/vtk/vtkm/{Find*.cmake,cmake/{Find*.cmake,3.15}}

}
